package com.wo.cloud.auth.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wo.cloud.admin.entity.SysUser;
import com.wo.cloud.auth.contant.AuthorizationConstant;
import com.wo.cloud.auth.entity.SysUserDetails;
import com.wo.cloud.auth.entity.User;
import com.wo.cloud.auth.mapper.SysUserMapper;
import com.wo.cloud.auth.mapper.UserMapper;
import com.wo.cloud.auth.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author liry
 * @version 1.0
 * @date Created on 2021/3/7 0:28
 * Description: 自定义用户密码登录
 */

@Service
public class UserDetailServiceImpl implements UserService {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Resource
    private UserMapper userMapper;

    @Resource
    private SysUserMapper sysUserMapper;

    /**
     * 模拟数据库登录, 整合前后端登录(在请求头里面进行区分)
     *
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        // 获取请求属性
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        // 获取request
        HttpServletRequest request = servletRequestAttributes.getRequest();

        String loginType = request.getHeader(AuthorizationConstant.LOGIN_TYPE);

        if (StrUtil.isEmpty(loginType)) {
            loginType = AuthorizationConstant.USER;
        }

        switch (loginType) {
            case AuthorizationConstant.USER:
                // 前台
                return userMapper.selectOne(new QueryWrapper<User>().eq("username", username));
            case AuthorizationConstant.SYS_USER:

                // 后台
                SysUserDetails sysUserDetails = sysUserMapper.selectOne(new LambdaQueryWrapper<SysUserDetails>().eq(SysUserDetails::getUsername, username));
                return getUserInfo(sysUserDetails);
        }
        return null;
    }

    @Override
    public UserDetails loadUserByMobile(String mobile) {

        SysUserDetails sysUserDetails = sysUserMapper.selectOne(new LambdaQueryWrapper<SysUserDetails>().eq(StrUtil.isNotBlank(mobile), SysUser::getMobile, mobile));
        return getUserInfo(sysUserDetails);
    }

    /**
     * 获取用户角色权限信息
     *
     * @param sysUserDetails
     * @return UserDetails
     */
    private UserDetails getUserInfo(SysUserDetails sysUserDetails) {
        if (ObjectUtil.isNotEmpty(sysUserDetails)) {
            // 获取用户角色
            List<String> roles = sysUserMapper.getRoleByUserId(sysUserDetails.getId());
            sysUserDetails.setRoles(roles);

            List<String> roleIds = sysUserMapper.getRoleIdsByUserId(sysUserDetails.getId());
            // 获取用户权限
            List<String> permissions = sysUserMapper.getPermissionByUserId(roleIds);
            sysUserDetails.setAuths(permissions);
        }
        return sysUserDetails;
    }
}
